﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App_Code;

public partial class ajax_DatHang : System.Web.UI.Page
{
    protected User oUser = null;
    dulieuDataContext db = new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        oUser = (User)Session["user"];
        LoadUserInfo();
    }
    private void LoadUserInfo()
    {
        if (oUser != null)
        {
            txthoten.Text = oUser.fullname;
            txtdienthoai.Text = oUser.mobile;
            txtemail.Text = oUser.Email;
            txtdiachi.Text = oUser.address;
        }
    }
    protected void Unnamed3_Click1(object sender, EventArgs e)
    {
        var cart = (Cart)(Session["cart"]);
        int quantity;
        double price;
        string idpr;
        var address = txtdiachi.Text;
        var date = DateTime.Now;
        int orderID = 0;// = (from o in db.Order1s select o.Order_ID).Max();
        var ordlist = from o in db.Orders orderby o.Order_ID descending select o;
        if (ordlist.ToList().Count != 0)
            orderID = ordlist.ToList()[0].Order_ID + 1;
        var order = new Order()
        {
            Order_ID = orderID,
            Name = txthoten.Text,
            Mobile = txtdienthoai.Text,
            Address = txtdiachi.Text,
            Email = txtemail.Text,
            Comment = txtnoidung.Text,
            CreateTime = date
        };
        db.Orders.InsertOnSubmit(order);
        db.SubmitChanges();
        for (int i = 0; i < cart.GetTotalPro(); i++)
        {
            idpr = cart.GetIdpr(i);
            price = cart.GetPrice(idpr);
            quantity = cart.GetQuantity(i);
            var orderDetail = new OrderDetail() { Order_ID = orderID, ProductID = Convert.ToDecimal(idpr), PriceOnline = price, Quantity = quantity };
            db.OrderDetails.InsertOnSubmit(orderDetail);
            db.SubmitChanges();
        }

        lblthongbao.Text = "Đã mua hàng thành công!";
    }
}